﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Guia.Elements;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Guia.Core;
using WpfTestApplication;

namespace GuiaTest
{
    /// <summary>
    /// Summary description for ComboBoxItemTest
    /// </summary>
    [TestClass]
    public class ListBoxItemTest : AbstractWindowTest
    {
        public ListBoxItemTest()
        {
        }

        
        [TestMethod]
        public void TestSelect()
        {
            ListBox listBox = window.Get<ListBox>("listBoxTest");
            ListBoxItem item1 = listBox.GetItem("item 2");
            ListBoxItem item2 = listBox.GetItem("item 3");
            Assert.IsFalse(item1.IsSelected);
            item1.Select();
            Assert.IsTrue(item1.IsSelected);
            item1.Unselect();
            Assert.IsFalse(item1.IsSelected);
            item1.Select();
            Assert.IsTrue(item1.IsSelected);
            item2.Select();
            Assert.IsTrue(item2.IsSelected);
            Assert.IsFalse(item1.IsSelected);
        }

        [TestMethod, ExpectedException(typeof(InvalidOperationException))]
        public void TestAddToSelectionUnsupported()
        {
            ListBox listBox = window.Get<ListBox>("listBoxTest");
            ListBoxItem item1 = listBox.GetItem("item 1");
            ListBoxItem item2 = listBox.GetItem("item 2");
            ListBoxItem item3 = listBox.GetItem("item 3");
            item1.AddToSelection();
            Assert.IsTrue(item1.IsSelected);
            Assert.IsFalse(item2.IsSelected);
            Assert.IsFalse(item3.IsSelected);
            item2.AddToSelection();
        }

        [TestMethod]
        public void TestGetItemsIndexer()
        {
            ListBox listBox = window.Get<ListBox>("listBoxTest");
            Assert.AreEqual("item 1", listBox[0].TextValue);
            Assert.AreEqual("item 2", listBox[1].TextValue);
            Assert.AreEqual("item 3", listBox[2].TextValue);
        }

        [TestMethod]
        [ExpectedException(typeof(ElementNotFoundException))]
        public void TestGetItemsIndexerNotFound()
        {
            ListBox listBox = window.Get<ListBox>("listBoxTest");
            Assert.AreEqual("item 1", listBox[3].TextValue);
        }

        [TestMethod]
        public void TestAddToSelection()
        {
            ListBox listBoxMultiSelect = window.Get<ListBox>("listBoxMultiSelect");
            ListBoxItem item1 = listBoxMultiSelect.GetItem("item 1");
            ListBoxItem item2 = listBoxMultiSelect.GetItem("item 2");
            ListBoxItem item3 = listBoxMultiSelect.GetItem("item 3");
            item1.AddToSelection();
            Assert.IsTrue(item1.IsSelected);
            Assert.IsFalse(item2.IsSelected);
            Assert.IsFalse(item3.IsSelected);
            item2.AddToSelection();
            Assert.IsTrue(item1.IsSelected);
            Assert.IsTrue(item2.IsSelected);
            Assert.IsFalse(item3.IsSelected);
            item3.AddToSelection();
            Assert.IsTrue(item1.IsSelected);
            Assert.IsTrue(item2.IsSelected);
            Assert.IsTrue(item3.IsSelected);
        }

        [TestMethod]
        public void TestUnselectInSingleSelect()
        {
            ListBox listBoxMultiSelect = window.Get<ListBox>("listBoxMultiSelect");
            ListBoxItem item1 = listBoxMultiSelect.GetItem("item 1");
            item1.AddToSelection();
            Assert.IsTrue(item1.IsSelected);
            item1.Unselect();
            Assert.IsFalse(item1.IsSelected);
        }

        [TestMethod]
        public void TestUnselectInMultiSelect()
        {
            ListBox listBoxMultiSelect = window.Get<ListBox>("listBoxMultiSelect");
            ListBoxItem item1 = listBoxMultiSelect.GetItem("item 1");
            ListBoxItem item2 = listBoxMultiSelect.GetItem("item 2");
            ListBoxItem item3 = listBoxMultiSelect.GetItem("item 3");
            item1.AddToSelection();
            item2.AddToSelection();
            item3.AddToSelection();
            Assert.IsTrue(item1.IsSelected);
            Assert.IsTrue(item2.IsSelected);
            Assert.IsTrue(item3.IsSelected);
            // now remove
            item2.Unselect();
            Assert.IsTrue(item1.IsSelected);
            Assert.IsFalse(item2.IsSelected);
            Assert.IsTrue(item3.IsSelected);
            item1.Unselect();
            item3.Unselect();
            Assert.IsFalse(item1.IsSelected);
            Assert.IsFalse(item2.IsSelected);
            Assert.IsFalse(item3.IsSelected);
        }
        

        [TestMethod]
        public void TestSetIsSelected()
        {
            ListBox listBox = window.Get<ListBox>("listBoxTest");
            ListBoxItem item = listBox.GetItem("item 3");
            item.IsSelected = false;
            Assert.IsFalse(item.IsSelected);
            item.IsSelected = true;
            Assert.IsTrue(item.IsSelected);
            item.IsSelected = true;
            Assert.IsTrue(item.IsSelected);
            item.IsSelected = false;
            Assert.IsFalse(item.IsSelected);
        }

        [TestMethod]
        public void TestGetTextValue()
        {
            ListBox listBox = window.Get<ListBox>("listBoxTest");
            ListBoxItem item = listBox.GetItem("item 1");
            Assert.AreEqual("item 1",item.TextValue);
        }

        protected override Type TestWindowType()
        {
            return typeof(ListBoxTestWindow);
        }

        protected override string TestWindowName()
        {
            return "ListBoxTestWindow";
        }
    }
}
